package com.baaio.util;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import com.baaio.xml.WriterConfig;

/**
 * 在没有congfig.xml配置文件的情况下，该类可以生产配置文件
 * 
 * @author Administrator
 * 
 */
public class AddConfigFrame extends JFrame
{

	private static final long serialVersionUID = 6389750432494290383L;
	private JButton b1, b2;
	private JTextField t1, t2, t3, t4;
	private JComboBox jcb1, jcb2;
	private JPasswordField t6;
	private JLabel lb1, lb2, lb3, lb4, lb5, lb6, lb7, lb8;
	private JPanel p1, p2, p3, p4, p5, p6, p7, p8, p9, p;

	/**
	 * 使用方法，AddConfigFrame af = new AddConfigFrame();
	 */
	public AddConfigFrame()
	{

		this.setTitle("数据库信息添加");
		String[] object = { "mysql", "sql","oldsql" };
		String[] object2 = { "true", "false" };
		FlowLayout f = new FlowLayout(FlowLayout.RIGHT);
		b1 = new JButton("确认");
		b2 = new JButton("重置");

		b1.addActionListener(new ActionListener()
		{

			public void actionPerformed(ActionEvent e)
			{

				String h, p, d, r, pw, sq, pr;
				h = t1.getText();
				p = t2.getText();

				d = t3.getText();
				r = t4.getText();
				pw = new String(t6.getPassword());
				sq = (String) jcb1.getSelectedItem();
				pr = (String) jcb2.getSelectedItem();
				if (h.equals(""))
				{
					JOptionPane.showMessageDialog(null, "数据库地址不能为空!");
					t1.requestFocus();
				}
				else if (p.equals(""))
				{
					JOptionPane.showMessageDialog(null, "端口号不能可为空!");
					t2.requestFocus();
				}
				else if (d.equals(""))
				{
					JOptionPane.showMessageDialog(null, "数据库名称不能为空!");
					t3.requestFocus();
				}
				else if (r.equals(""))
				{
					JOptionPane.showMessageDialog(null, "用户名不能为空!");
					t4.requestFocus();
				}
				else if (pw.equals(""))
				{
					int k = JOptionPane.showConfirmDialog(null, "密码为空？", "警告！",
							JOptionPane.NO_OPTION);
					if (k != 0)
					{
						t6.requestFocus();
					}
					else
					{
						new WriterConfig(h, p, d, r, pw, sq, pr);
						JOptionPane.showMessageDialog(null,
								"配置文件config.xml已经生成，请刷新项目！");
						System.exit(0);
					}
				}
				else
				{
					new WriterConfig(h, p, d, r, pw, sq, pr);
					JOptionPane.showMessageDialog(null,
							"配置文件config.xml已经生成，请刷新项目！");
					System.exit(0);
				}

			}
		});
		b2.addActionListener(new ActionListener()
		{

			public void actionPerformed(ActionEvent e)
			{

				clear();

			}
		});

		t1 = new JTextField(10);
		t2 = new JTextField(10);
		t2.setDocument(new NumOnlyDocument());

		t3 = new JTextField(10);
		t4 = new JTextField(10);
		t6 = new JPasswordField(10);

		jcb1 = new JComboBox(object);
		jcb2 = new JComboBox(object2);

		lb1 = new JLabel("添加数据库信息:");
		lb2 = new JLabel("数据库地址：");
		lb3 = new JLabel("端口号:");
		lb4 = new JLabel("数据库名称:");
		lb5 = new JLabel("用户名:");
		lb6 = new JLabel("密码:");
		lb7 = new JLabel("数据库类型:");
		lb8 = new JLabel("控制台输出:");

		p1 = new JPanel();
		p2 = new JPanel(f);
		p3 = new JPanel(f);
		p4 = new JPanel(f);
		p5 = new JPanel(f);
		p6 = new JPanel(f);
		p7 = new JPanel();
		p8 = new JPanel();
		p9 = new JPanel();
		p = new JPanel();

		p1.add(lb1);

		p2.add(lb2);
		p2.add(t1);

		p3.add(lb3);
		p3.add(t2);

		p4.add(lb4);
		p4.add(t3);

		p5.add(lb5);
		p5.add(t4);

		p6.add(lb6);
		p6.add(t6);

		p7.add(lb7);
		p7.add(jcb1);

		p8.add(lb8);
		p8.add(jcb2);

		p.add(b1);
		p.add(b2);

		p9.add(p1);
		p9.add(p2);
		p9.add(p3);
		p9.add(p4);
		p9.add(p5);
		p9.add(p6);
		p9.add(p7);
		p9.add(p8);
		p9.add(p);
		p9.setBorder(BorderFactory.createTitledBorder("数据库信息"));
		p9.setLayout(new GridLayout(9, 1));
		this.add(p9);
		this.pack();
		this.setVisible(true);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// TODO Auto-generated constructor stub
	}

	private void clear()
	{

		t1.setText("");
		t2.setText("");
		t3.setText("");
		t4.setText("");
		t6.setText("");
	}

	public static void main(String[] args)
	{

		new AddConfigFrame();
	}

	class NumOnlyDocument extends PlainDocument
	{

		/**
		 * 
		 */
		private static final long serialVersionUID = -1713391340629804404L;

		public void insertString(int offset, String s, AttributeSet attrSet)
				throws BadLocationException
		{

			try
			{
				Integer.parseInt(s);
			}
			catch (NumberFormatException ex)
			{
				return;
			}
			super.insertString(offset, s, attrSet);
		}
	}
}
